home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d982.lha / HWGRCS / HWGpatchP1_2_1.lha / patch-2.1 / src / inp.c < prev    next >
C/C++ Source or Header  |  1993-12-18  |  12KB  |  379 lines

  1. /* $Header: inp.c,v 2.0.1.1 88/06/03 15:06:13 lwall Locked $
  2.  *
  3.  * $Log:        inp.c,v $
  4.  * Revision 2.0.1.1  88/06/03  15:06:13  lwall
  5.  * patch10: made a little smarter about sccs files
  6.  *
  7.  * Revision 2.0  86/09/17  15:37:02  lwall
  8.  * Baseline for netwide release.
  9.  *
  10.  */
  11.  
  12. #include "EXTERN.h"
  13. #include "common.h"
  14. #include "util.h"
  15. #include "pch.h"
  16. #include "INTERN.h"
  17. #include "inp.h"
  18.  
  19. /* Input-file-with-indexable-lines abstract type */
  20.  
  21. static long i_size;                     /* size of the input file */
  22. static char *i_womp;                    /* plan a buffer for entire file */
  23. static char **i_ptr;                    /* pointers to lines in i_womp */
  24.  
  25. static int tifd = -1;                   /* plan b virtual string array */
  26. static char *tibuf[2];                  /* plan b buffers */
  27. static LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  28. static LINENUM lines_per_buf;           /* how many lines per buffer */
  29. static int tireclen;                    /* length of records in tmp file */
  30.  
  31. /* New patch--prepare to edit another file. */
  32.  
  33. void
  34. re_input()
  35. {
  36.     if (using_plan_a) {
  37.         i_size = 0;
  38. #ifndef lint
  39.         if (i_ptr != Null(char**))
  40.             free((char *)i_ptr);
  41. #endif
  42.         if (i_womp != Nullch)
  43.             free(i_womp);
  44.         i_womp = Nullch;
  45.         i_ptr = Null(char **);
  46.     }
  47.     else {
  48.         using_plan_a = TRUE;            /* maybe the next one is smaller */
  49.         Close(tifd);
  50.         tifd = -1;
  51.         free(tibuf[0]);
  52.         free(tibuf[1]);
  53.         tibuf[0] = tibuf[1] = Nullch;
  54.         tiline[0] = tiline[1] = -1;
  55.         tireclen = 0;
  56.     }
  57. }
  58.  
  59. /* Constuct the line index, somehow or other. */
  60.  
  61. void
  62. scan_input(filename)
  63. char *filename;
  64. {
  65.     if (!plan_a(filename))
  66.         plan_b(filename);
  67.     if (verbose) {
  68.         say3("Patching file %s using Plan %s...\n", filename,
  69.           (using_plan_a ? "A" : "B") );
  70.     }
  71. }
  72.  
  73. /* Try keeping everything in memory. */
  74.  
  75. bool
  76. plan_a(filename)
  77. char *filename;
  78. {
  79.     int ifd, statfailed;
  80.     Reg1 char *s;
  81.     Reg2 LINENUM iline;
  82.     char lbuf[MAXLINELEN];
  83.     int output_elsewhere = strcmp(filename, outname);
  84.  
  85.     statfailed = stat(filename, &filestat);
  86.     if (statfailed && ok_to_create_file) {
  87.         if (verbose)
  88.             say2("(Creating file %s...)\n",filename);
  89.         makedirs(filename, TRUE);
  90.         close(creat(filename, 0666));
  91.         statfailed = stat(filename, &filestat);
  92.     }
  93.     /* For nonexistent or read-only files, look for RCS or SCCS versions.  */
  94.     if (statfailed
  95.         || (! output_elsewhere
  96.             && (/* No one can write to it.  */
  97. #ifdef _AMIGA
  98.                 /* I can't write to it.  */
  99.                 ((filestat.st_mode & S_IWRITE) == 0
  100. #else
  101.                 (filestat.st_mode & 0222) == 0
  102.                 /* I can't write to it.  */
  103.                 || ((filestat.st_mode & 0022) == 0
  104. #endif
  105.                     && filestat.st_uid != myuid)))) {
  106.         struct stat cstat;
  107.         char *cs = Nullch;
  108.         char *filebase;
  109.         int pathlen;
  110.  
  111.         filebase = basename(filename);
  112.         pathlen = filebase - filename;
  113.  
  114.         /* Put any leading path into `s'.
  115.            Leave room in lbuf for the diff command.  */
  116.         s = lbuf + 20;
  117.         strncpy(s, filename, pathlen);
  118.  
  119. #define try(f,a1,a2) (Sprintf(s + pathlen, f, a1, a2), stat(s, &cstat) == 0)
  120.         if ((   try("RCS/%s%s", filebase, RCSSUFFIX)
  121.              || try("RCS/%s"  , filebase,         0)
  122.              || try(    "%s%s", filebase, RCSSUFFIX))
  123.             &&
  124.             /* Check that RCS file is not working file.
  125.                Some hosts don't report file name length errors.  */
  126.             (statfailed
  127.              || (  (filestat.st_dev ^ cstat.st_dev)
  128.                  | (filestat.st_ino ^ cstat.st_ino)))) {
  129.             Sprintf(buf, output_elsewhere?CHECKOUT:CHECKOUT_LOCKED, filename);
  130.             Sprintf(lbuf, RCSDIFF, filename);
  131.             cs = "RCS";
  132.         } else if (   try("SCCS/%s%s", SCCSPREFIX, filebase)
  133.                    || try(     "%s%s", SCCSPREFIX, filebase)) {
  134.             Sprintf(buf, output_elsewhere?GET:GET_LOCKED, s);
  135.             Sprintf(lbuf, SCCSDIFF, s, filename);
  136.             cs = "SCCS";
  137.         } else if (statfailed)
  138.             fatal2("can't find %s\n", filename);
  139.         /* else we can't write to it but it's not under a version
  140.            control system, so just proceed.  */
  141.         if (cs) {
  142.             if (!statfailed) {
  143. #ifdef _AMIGA
  144.                 if ((filestat.st_mode & S_IWRITE) != 0)
  145. #else
  146.                 if ((filestat.st_mode & 0222) != 0)
  147. #endif
  148.                     /* The owner can write to it.  */
  149.                     fatal3("file %s seems to be locked by somebody else under %s\n",
  150.                            filename, cs);
  151.                 /* It might be checked out unlocked.  See if it's safe to
  152.                    check out the default version locked.  */
  153.                 if (verbose)
  154.                     say3("Comparing file %s to default %s version...\n",
  155.                          filename, cs);
  156.                 if (system(lbuf))
  157.                     fatal3("can't check out file %s: differs from default %s version\n",
  158.                            filename, cs);
  159.             }
  160.             if (verbose)
  161.                 say3("Checking out file %s from %s...\n", filename, cs);
  162.             if (system(buf) || stat(filename, &filestat))
  163.                 fatal3("can't check out file %s from %s\n", filename, cs);
  164.         }
  165.     }
  166.     filemode = filestat.st_mode;
  167.     if (!S_ISREG(filemode))
  168.         fatal2("%s is not a normal file--can't patch\n", filename);
  169.     i_size = filestat.st_size;
  170.     if (out_of_mem) {
  171.         set_hunkmax();          /* make sure dynamic arrays are allocated */
  172.         out_of_mem = FALSE;
  173.         return FALSE;                   /* force plan b because plan a bombed */
  174.     }
  175. #ifdef lint
  176.     i_womp = Nullch;
  177. #else
  178.     i_womp = malloc((MEM)(i_size+2));   /* lint says this may alloc less than */
  179.                                         /* i_size, but that's okay, I think. */
  180. #endif
  181.     if (i_womp == Nullch)
  182.         return FALSE;
  183.     if ((ifd = open(filename, 0)) < 0)
  184.         pfatal2("can't open file %s", filename);
  185. #ifndef lint
  186.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  187.         Close(ifd);     /* probably means i_size > 15 or 16 bits worth */
  188.         free(i_womp);   /* at this point it doesn't matter if i_womp was */
  189.         return FALSE;   /*   undersized. */
  190.     }
  191. #endif
  192.     Close(ifd);
  193.     if (i_size && i_womp[i_size-1] != '\n')
  194.         i_womp[i_size++] = '\n';
  195.     i_womp[i_size] = '\0';
  196.  
  197.     /* count the lines in the buffer so we know how many pointers we need */
  198.  
  199.     iline = 0;
  200.     for (s=i_womp; *s; s++) {
  201.         if (*s == '\n')
  202.             iline++;
  203.     }
  204. #ifdef lint
  205.     i_ptr = Null(char**);
  206. #else
  207.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  208. #endif
  209.     if (i_ptr == Null(char **)) {       /* shucks, it was a near thing */
  210.         free((char *)i_womp);
  211.         return FALSE;
  212.     }
  213.  
  214.     /* now scan the buffer and build pointer array */
  215.  
  216.     iline = 1;
  217.     i_ptr[iline] = i_womp;
  218.     for (s=i_womp; *s; s++) {
  219.         if (*s == '\n')
  220.             i_ptr[++iline] = s+1;       /* these are NOT null terminated */
  221.     }
  222.     input_lines = iline - 1;
  223.  
  224.     /* now check for revision, if any */
  225.  
  226.     if (revision != Nullch) {
  227.         if (!rev_in_string(i_womp)) {
  228.             if (force) {
  229.                 if (verbose)
  230.                     say2(
  231. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  232.                         revision);
  233.             }
  234.             else if (batch) {
  235.                 fatal2(
  236. "this file doesn't appear to be the %s version--aborting.\n", revision);
  237.             }
  238.             else {
  239.                 ask2(
  240. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  241.                     revision);
  242.             if (*buf != 'y')
  243.                 fatal1("aborted\n");
  244.             }
  245.         }
  246.         else if (verbose)
  247.             say2("Good.  This file appears to be the %s version.\n",
  248.                 revision);
  249.     }
  250.     return TRUE;                        /* plan a will work */
  251. }
  252.  
  253. /* Keep (virtually) nothing in memory. */
  254.  
  255. void
  256. plan_b(filename)
  257. char *filename;
  258. {
  259.     Reg3 FILE *ifp;
  260.     Reg1 int i = 0;
  261.     Reg2 int maxlen = 1;
  262.     Reg4 bool found_revision = (revision == Nullch);
  263.  
  264.     using_plan_a = FALSE;
  265.     if ((ifp = fopen(filename, "r")) == Nullfp)
  266.         pfatal2("can't open file %s", filename);
  267.     if ((tifd = creat(TMPINNAME, 0666)) < 0)
  268.         pfatal2("can't open file %s", TMPINNAME);
  269.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  270.         if (revision != Nullch && !found_revision && rev_in_string(buf))
  271.             found_revision = TRUE;
  272.         if ((i = strlen(buf)) > maxlen)
  273.             maxlen = i;                 /* find longest line */
  274.     }
  275.     if (revision != Nullch) {
  276.         if (!found_revision) {
  277.             if (force) {
  278.                 if (verbose)
  279.                     say2(
  280. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  281.                         revision);
  282.             }
  283.             else if (batch) {
  284.                 fatal2(
  285. "this file doesn't appear to be the %s version--aborting.\n", revision);
  286.             }
  287.             else {
  288.                 ask2(
  289. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  290.                     revision);
  291.                 if (*buf != 'y')
  292.                     fatal1("aborted\n");
  293.             }
  294.         }
  295.         else if (verbose)
  296.             say2("Good.  This file appears to be the %s version.\n",
  297.                 revision);
  298.     }
  299.     Fseek(ifp, 0L, 0);          /* rewind file */
  300.     lines_per_buf = BUFFERSIZE / maxlen;
  301.     tireclen = maxlen;
  302.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  303.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  304.     if (tibuf[1] == Nullch)
  305.         fatal1("out of memory\n");
  306.     for (i=1; ; i++) {
  307.         if (! (i % lines_per_buf))      /* new block */
  308.             if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  309.                 pfatal1("can't write temp file");
  310.         if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  311.           == Nullch) {
  312.             input_lines = i - 1;
  313.             if (i % lines_per_buf)
  314.                 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  315.                     pfatal1("can't write temp file");
  316.             break;
  317.         }
  318.     }
  319.     Fclose(ifp);
  320.     Close(tifd);
  321.     if ((tifd = open(TMPINNAME, 0)) < 0) {
  322.         pfatal2("can't reopen file %s", TMPINNAME);
  323.     }
  324. }
  325.  
  326. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  327.  
  328. char *
  329. ifetch(line,whichbuf)
  330. Reg1 LINENUM line;
  331. int whichbuf;                           /* ignored when file in memory */
  332. {
  333.     if (line < 1 || line > input_lines)
  334.         return "";
  335.     if (using_plan_a)
  336.         return i_ptr[line];
  337.     else {
  338.         LINENUM offline = line % lines_per_buf;
  339.         LINENUM baseline = line - offline;
  340.  
  341.         if (tiline[0] == baseline)
  342.             whichbuf = 0;
  343.         else if (tiline[1] == baseline)
  344.             whichbuf = 1;
  345.         else {
  346.             tiline[whichbuf] = baseline;
  347. #ifndef lint            /* complains of long accuracy */
  348.             Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  349. #endif
  350.             if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  351.                 pfatal2("error reading tmp file %s", TMPINNAME);
  352.         }
  353.         return tibuf[whichbuf] + (tireclen*offline);
  354.     }
  355. }
  356.  
  357. /* True if the string argument contains the revision number we want. */
  358.  
  359. bool
  360. rev_in_string(string)
  361. char *string;
  362. {
  363.     Reg1 char *s;
  364.     Reg2 int patlen;
  365.  
  366.     if (revision == Nullch)
  367.         return TRUE;
  368.     patlen = strlen(revision);
  369.     if (strnEQ(string,revision,patlen) && isspace(string[patlen]))
  370.         return TRUE;
  371.     for (s = string; *s; s++) {
  372.         if (isspace(*s) && strnEQ(s+1, revision, patlen) &&
  373.                 isspace(s[patlen+1] )) {
  374.             return TRUE;
  375.         }
  376.     }
  377.     return FALSE;
  378. }
  379.